package com.example.webbrowser.Fragment;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.webbrowser.Adapter.FragmentAdapter;
import com.example.webbrowser.R;
import com.example.webbrowser.WebViewActivity;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;


public class HomeFragment extends Fragment {
    private View view;//定义view用来设置fragment的layout

    private EditText text_url;
    private Button button_run;
    //增加删除全文按钮
    private ImageView delete_image;

    private TabLayout tabLayout;
    private ViewPager2 viewPager;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.home, container, false);
        initView();
        initData();
        return view;
    }

    private void initView() {
        tabLayout = view.findViewById(R.id.tabLayout);
        viewPager = view.findViewById(R.id.vp2);
    }

    private void initData() {
        viewPager.setAdapter(new FragmentAdapter(getActivity()));

        TabLayoutMediator tab = new TabLayoutMediator(tabLayout, viewPager, (tab1, position) -> {
            switch (position) {
                case 0:
                    tab1.setText("娱乐");
                    break;
                case 1:
                    tab1.setText("IT");
                    break;
                case 2:
                    tab1.setText("互联网");
                    break;
            }
        });
        tab.attach();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        text_url = (EditText) view.findViewById(R.id.web_url);
        button_run = (Button) view.findViewById(R.id.run);

        //增加删除全文按钮
        delete_image = (ImageView) view.findViewById(R.id.delete);

        button_run.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(text_url.getText())){
                    Intent intent = new Intent(getContext(), WebViewActivity.class);
                    String str = text_url.getText().toString();
                    intent.putExtra("web_url",str);
                    startActivity(intent);
                }else{
                    Toast.makeText(getActivity(),"输入内容为空",Toast.LENGTH_SHORT).show();
                }
            }
        });

        //监听textView中是否有内容,决定删除按钮是否显示
        text_url.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            //长度为0说明是空,隐藏,反之则显示
            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() == 0) {
                    delete_image.setVisibility(View.GONE);
                } else {
                    delete_image.setVisibility(View.VISIBLE);
                }
            }
        });

        //点击删除输入框中的内容
        delete_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text_url.setText("");
            }
        });

        /********************键盘回车键搜索*********************/
        text_url.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        text_url.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    button_run.callOnClick();
                    return true;
                }
                return false;
            }
        });
    }
}